
import gradio as gr
# SDK引入模型
from dwspark.config import Config
from dwspark.models import Text2Audio
# 日志
# 加载系统环境变量：SPARKAI_UID、SPARKAI_APP_ID、SPARKAI_API_KEY、SPARKAI_API_SECRET
config = Config()
model = Text2Audio(config)

file_path = './audio.mp3'
def gen_audio(chat_query: str):
    '''
    大模型对话
    :param chat_query: 当前用户问题
    :param chat_history: 历史对话
    :return:
    '''

    audio_result = model.generate(chat_query, file_path)
    if audio_result is True:
        return file_path
    else:
        return None



# gradio页面绘制
with gr.Blocks() as demo:
    # 聊天对话框
    audio = gr.Audio(type='filepath', label="合成语音结果")
    # 输入框
    chat_query = gr.Textbox(label="输入问题", placeholder="输入需要咨询的问题")
    # 按钮
    llm_submit_tab = gr.Button("发送", visible=True)
    # 问题样例
    gr.Examples(["你好，这里是datawhale团队，请问有什么可以帮你的？"], chat_query)
    # 按钮出发逻辑
    llm_submit_tab.click(fn=gen_audio, inputs=[chat_query], outputs=[audio])

if __name__ == "__main__":
    demo.queue().launch()
